package com.burgeon.backend.EnterpriseWeChat.rest;

import com.burgeon.backend.EnterpriseWeChat.entity.RFIMVipCount;
import com.burgeon.backend.EnterpriseWeChat.entity.YcValueAction;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

public class GetYcValueActionByEmpId extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public GetYcValueActionByEmpId(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<YcValueAction> getData(String empId) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(empId), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<YcValueAction> ycValueActions= new ArrayList<>();
        if(jsonArray.length()>0){
            for (int i = 0; i <jsonArray.length() ; i++) {
                YcValueAction ycValueAction = new YcValueAction();
                DecimalFormat df2 = new DecimalFormat("0");
                ycValueAction.setVipName(jsonArray.getJSONArray(i).getString(0));
                ycValueAction.setAverage(Integer.parseInt(df2.format(jsonArray.getJSONArray(i).getDouble(1))));
                ycValueAction.setDescription(jsonArray.getJSONArray(i).getString(2));
                ycValueAction.setCreationDate(jsonArray.getJSONArray(i).getString(3));
                ycValueAction.setVipId(jsonArray.getJSONArray(i).getString(4));
//                ycValueAction.setVipLogo(jsonArray.getJSONArray(i).getString(5));
                if("null".equals(jsonArray.getJSONArray(0).getString(5))){
                    ycValueAction.setVipLogo("http://lfwdcdn.cloudsmicro.com/img/EnterpriseWeChat/vip/app.png");
                }else {
                    ycValueAction.setVipLogo(jsonArray.getJSONArray(0).getString(5));
                }
                ycValueActions.add(ycValueAction);
            }
        }

        return ycValueActions;
    }

    private MultiValueMap Data(String empId) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("'C_VIP_ID;VIPNAME'");
        colums.add("AVERAGE");//评分
        colums.add("DESCRIPTION");
        colums.add("CREATIONDATE");
        colums.add("'C_VIP_ID'");
        colums.add("'C_VIP_ID;LOGO_URL'");

        params.put("columns", colums);
        params.put("table", "YC_VALUATION");//表名
        params.put("count", "true");

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        expr1.put("column","'C_VIP_ID;HR_EMPLOYEE_ID1'");
        expr1.put("condition",empId);//["condition"] = guiderId
        //expr1["column"] = "'C_VIP_ID;HR_EMPLOYEE_ID1'"

        HashMap expr2 = new HashMap();

        expr2.put("column","'C_VIP_ID'");
        expr2.put("condition","is not null");//["condition"] = guiderId


        paramss.put("expr1",expr1);
        paramss.put("expr2",expr2);
        paramss.put("combine","and");

        params.put("params", paramss);
//        System.err.println(paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetYcValueActionByEmpId g = new GetYcValueActionByEmpId("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("7215"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
